package com.web4code.codegen;

import com.web4code.codegen.config.GenConfig;
import com.web4code.codegen.generator.ProjectUtils;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * 代码生成器应用入口
 * 
 * @author web4code
 */
public class CodeGeneratorApp extends Application {
    public static void main(String[] args) {
        final String cmdLineSyntax = "codegen [options] [cmd] [argments]";
        final String cmdLineFooter = "Commands:\n"
            + " init TPLNAME PRJNAME  init a project from template.\n"
            + " list                  list available templates.";
        // create the command line parser
        CommandLineParser parser = new DefaultParser();
        // create the Options
        final Options options = new Options();
        options.addOption("c", "config", true, "config file. default ./codegen.yaml");
        options.addOption("h", "help", false, "print this message.");
        options.addOption("v", "version", false, "print the version number.");
        //
        CommandLine cmd = null;
        try {
            // parse the command line arguments
            cmd = parser.parse(options, args);
        } catch (ParseException e) {
            e.printStackTrace();
            return;
        }
        if (cmd.hasOption("version")) {
            System.out.println("codegen v1.0.0");
            return;
        }
        if (cmd.hasOption("help")) {
            // automatically generate the help statement
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(cmdLineSyntax, "", options, cmdLineFooter);
            return;
        }
        // 加载配置文件
        String configFile = "codegen.yaml";
        if (cmd.hasOption("config")) {
            configFile = cmd.getOptionValue("config");
        }
        GenConfig.loadConfigFile(configFile);
        // 优先使用命令行参数配置
        final String[] argArray = cmd.getArgs();
        boolean commandResult = false;
        if (argArray != null && argArray.length > 0) {
            commandResult = dealCommandLine(argArray);
            if (!commandResult) {
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp(cmdLineSyntax, "", options, cmdLineFooter);
            }
            return;
        }
        // 运行GUI图形界面
        launch(args);
    }

    private static boolean dealCommandLine(final String[] argArray) {
        final String argCommand = argArray[0];
        if ("init".equals(argCommand)) {
            if (argArray.length < 3) {
                System.out.println("command init argument not enough.");
                return false;
            }
            return ProjectUtils.initProjectFromTemplate(argArray[1], argArray[2]);
        }
        else if ("list".equals(argCommand)) {
            return ProjectUtils.listProjectTemplates();
        }
        return false;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("main.fxml"));
        Scene scene = new Scene(root, 600, 400);
        scene.getStylesheets().add(getClass().getClassLoader().getResource("application.css").toExternalForm());
        //
        primaryStage.setTitle("代码生成器");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
